/**
 * @author Chad Dettmering
 */

package org.flckphoto;


import java.io.*;
import java.util.Scanner;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Starting activity of program. Shows main menu.
 */
public class MainMenu extends Activity {
	private static final String TAG = "MainMenu";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LoadSettings();
        Button cameraButton = (Button) findViewById(R.id.mainTakePictureButton);
        cameraButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		Intent cameraIntent = new Intent(getApplicationContext(), CameraManager.class);
        		startActivity(cameraIntent);
        	}
        });
        
        Button galleryButton = (Button) findViewById(R.id.mainGalleryButton);
        galleryButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		Intent galleryIntent = new Intent(getApplicationContext(), Gallery.class);
        		startActivity(galleryIntent);
        	}
        });
    }
    
    /**
     * overrides the creation of the submenu, to add in our
     * "settings" option
     * 
     * @param menu the menu to create the options on
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflate = getMenuInflater();
    	inflate.inflate(R.menu.main_options, menu);
    	return true;
    }
    
    /**
     * overrides the selection of menu items, to handle
     * the selection of our particular menu
     * 
     * @param item the item that is currently being selected
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
	    	case R.id.settingsItem:
	    		Intent intent = new Intent(getApplicationContext(), 
	    									SettingsActivity.class);
	    		startActivity(intent);
	    		break;
    	}
    	
    	return true;
    }
    
    /**
     * Loads the settings from settings.config,
     * before the application does anything
     */
    private void LoadSettings() {
    	Util.METRICS = new DisplayMetrics();
    	getWindowManager().getDefaultDisplay().getMetrics(Util.METRICS);
    	try {
    		FileInputStream f = openFileInput(Settings.FILENAME);
    		Scanner scan = new Scanner(f);
    		
    		//check to see if there is any data in the settings file
    		if(scan.hasNext()) {
	    		Settings.username = scan.next();
	    		
	    		if(scan.hasNext())
	    			Settings.password = scan.next();
	    		
	    		if(scan.hasNext())
	    			Settings.authToken = scan.next();
	    		
	    		/*if(FlickrManager.getSingleton().isAuthenticated())
	    			Log.d(TAG, "Authentication token detected, and it is valid." + Settings.authToken);
	    		else
	    			Log.d(TAG, "Authentication token detected, and it is invalid: " + Settings.authToken);
	    			*/
    		}
    		else {
    			//settings file exists, but no data is stored in it!
    		}
    		
    		scan.close();
    		f.close();
    		
    	} catch(FileNotFoundException e){
    		//settings don't exist!
    		//call Flickr login screen here
    		try {
	    		FileOutputStream f2 = openFileOutput(Settings.FILENAME, MODE_PRIVATE);
	    		Settings.createDefaultSettings(f2);
    		}
    		catch(FileNotFoundException d) {
    		}
    		catch(IOException f) {
    			
    		}
    	}
    	catch(IOException e) {
    		
    	}
    }
}